package com.link.ppt.View.Adapter.Item;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.google.gson.Gson;
import com.link.ppt.Common.RecyclerView.BaseItem;
import com.link.ppt.Consts.Constants;
import com.link.ppt.Model.Bean.PPTEntity;
import com.link.ppt.R;
import com.link.ppt.Utils.DisplayUtil;
import com.link.ppt.Utils.GlideUtils;
import com.link.ppt.View.PPTWebviewActivity;

import de.hdodenhof.circleimageview.CircleImageView;


/**
 * Created by liucxu on 2017/8/10.
 */

public class PPTStoreItem extends BaseItem<PPTEntity> {

    PPTEntity resultsBean;
    ImageView imv_ppt_image;
    CircleImageView imv_header;
    TextView tv_publish_name;
    TextView tv_publish_time;
    RatingBar ratingbar_score;
    TextView tv_ppt_title;

    public PPTStoreItem(ViewGroup parent) {
        super(parent);
    }

    @Override
    public int getRes() {
        return R.layout.adapter_main_ppt;
    }

    @Override
    public void onCreateView(View rootView) {
        imv_ppt_image = (ImageView) rootView.findViewById(R.id.imv_ppt_image);
        imv_header = (CircleImageView) rootView.findViewById(R.id.imv_header);
        tv_publish_name = (TextView) rootView.findViewById(R.id.tv_publish_name);
        tv_publish_time = (TextView) rootView.findViewById(R.id.tv_publish_time);
        ratingbar_score = (RatingBar) rootView.findViewById(R.id.ratingbar_score);
        tv_ppt_title=(TextView)rootView.findViewById(R.id.tv_ppt_title);
    }

    @Override
    public void onBindView(PPTEntity entity) {
        this.resultsBean = entity;
        ViewGroup.LayoutParams para;
        para = imv_ppt_image.getLayoutParams();
        para.height = 9 * (DisplayUtil.GetDisplayWidth() - DisplayUtil.dip2px(getContext(), 30)) / 16;
        para.width = DisplayUtil.GetDisplayWidth() - DisplayUtil.dip2px(getContext(), 30);
        imv_ppt_image.setLayoutParams(para);
        if (!TextUtils.isEmpty(resultsBean.getPptImgUrl()) && resultsBean.getPptImgUrl().startsWith("http")) {
//            GlideUtils.getInstance().LoadContextBitmap(getContext(), resultsBean.getPptImgUrl(), imv_ppt_image, DisplayUtil.dip2px(getContext(), 10), R.mipmap.default_ppt_bg, R.mipmap.default_ppt_bg);
            GlideUtils.getInstance().LoadContextBitmap(getContext(), resultsBean.getPptImgUrl(), imv_ppt_image, R.mipmap.default_ppt_bg, R.mipmap.default_ppt_bg, GlideUtils.LOAD_BITMAP);
        } else {
//            GlideUtils.getInstance().LoadContextRoundBitmap(getContext(), Constants.BASE_URL+resultsBean.getPptImgUrl(), imv_ppt_image, DisplayUtil.dip2px(getContext(), 10), R.mipmap.default_ppt_bg, R.mipmap.default_ppt_bg);
            GlideUtils.getInstance().LoadContextBitmap(getContext(), Constants.BASE_URL + resultsBean.getPptImgUrl(), imv_ppt_image, R.mipmap.default_ppt_bg, R.mipmap.default_ppt_bg, GlideUtils.LOAD_BITMAP);
        }
//        GlideUtils.getInstance().loadSimpleContextVitMap(getContext(), Constants.BASE_URL + resultsBean.getHeadImgUrl(), imv_header);
        GlideUtils.getInstance().LoadContextBitmap(getContext(),Constants.BASE_URL + resultsBean.getHeadImgUrl(),imv_header,R.mipmap.default_comment_header,R.mipmap.default_comment_header,GlideUtils.LOAD_BITMAP);
        tv_publish_name.setText(resultsBean.getFromUserName());
        tv_publish_time.setText(resultsBean.getCreateTime());
        if(TextUtils.isEmpty(entity.getPoint())){
            ratingbar_score.setVisibility(View.INVISIBLE);
        }
        else{
            ratingbar_score.setVisibility(View.VISIBLE);
            ratingbar_score.setRating(Float.parseFloat(entity.getPoint()+""));
        }
        tv_ppt_title.setText(resultsBean.getPptTitle());
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getContext(), PPTWebviewActivity.class);
        Gson gson = new Gson();
        intent.putExtra("pptId", resultsBean.getPptId());
        getContext().startActivity(intent);
    }
}
